package com.app.micaihu.view.login.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.infor.UserInfor
import com.app.micaihu.configure.TypeConstant
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ApiServiceUtils
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.bean.AccountConflict
import com.app.micaihu.view.login.contract.LogInContract
import com.app.micaihu.view.utils.ParamsMap
import com.app.utils.biz.Biz
import com.app.utils.util.AppTools

open class LogInPresenter : BasePresenter<LogInContract.View>(), LogInContract.Presenter {


    override fun quickLogin(openId: String?, nickName: String?, headPic: String?, fromType: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("nickname", nickName)
        params.putValue("openid", openId)
        params.putValue("avatar", headPic)
        params.putValue("fromType", fromType)
        params.putValue("thirdInfo", "")
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        params.putValue("version", AppTools.getVersionCode())
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.quickLogin(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleLogin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleLogin(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    /**
     * 绑定第三方账户
     * @param  string fromType 登录类型：(1微信登录，2qq登录, 3手机号，4微博)
     * @param  string openid 第三方用户openId
     * @param  string nickname 第三方昵称
     * @param  string avatar 第三方头像
     * @param  string thirdInfo 描述信息
     * @param  string military 军种 1陆军，2海军，3空军，4火箭军，5战略支援，6武警
     * @param  string masterUid 主账户ID
     */
    override fun boundThirdUser(openId: String?, nickName: String?, headPic: String?, fromType: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("nickname", nickName)
        params.putValue("openid", openId)
        params.putValue("avatar", headPic)
        params.putValue("fromType", fromType)
        params.putValue("thirdInfo", "")
        params.putValue("masterUid", Biz.instance.uid())
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        params.putValue("version", AppTools.getVersionCode())
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.boundThirdUser(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleBoundThirdUser(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleBoundThirdUser(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun login(mobile: String, password: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("mobile", mobile)
        params.putValue("password", password)
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("version", AppTools.getVersionCode())
        params.putValue("type", TypeConstant.PLATFORM)
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.login(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleLogin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleLogin(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun flashLogin(token: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("token", token)
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        params.putValue("version", AppTools.getVersionCode())
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.flashLogin(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleFlashLogin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleFlashLogin(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun captchaLogin(mobile: String, captcha: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("mobile", mobile)
        params.putValue("captcha", captcha)
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        params.putValue("version", AppTools.getVersionCode())
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.captchaLogin(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleCaptchaLogin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleCaptchaLogin(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun weiLogin(userId: String) {
        val params = ParamsMap()
        params.putValue("uid", userId)
        params.putValue("version", AppTools.getVersionCode())
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        if (Biz.instance.military.isNotEmpty()) {
            params.putValue("military", Biz.instance.military)
        }
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.weiLogin(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleLogin(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleLogin(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun sendCode(mobile: String, type: String) {
        getView()?.showLoading()
        val params = ParamsMap()
        params.putValue("mobile", mobile)
        params.putValue("type", type)
        params.putValue("newMobile", "")
        params.putValue("uid", "")
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.sendCode(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleSendCode(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleSendCode(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })

    }

    override fun checkMobile(mobile: String) {
        val params = ParamsMap()
        params.putValue("mobile", mobile)
        params.putValue("type", TypeConstant.CHANGE_PWD)
        params.putValue("newMobile", "")
        params.putValue("uid", Biz.instance.uid())
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.checkMobile(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleCheckMobile(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleCheckMobile(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun changePassword(mobile: String, captcha: String, password: String, type: String) {
        val params = ParamsMap()
        params.putValue("mobile", mobile)
        params.putValue("password", password)
        params.putValue("type", type)
        params.putValue("captcha", captcha)
        params.putValue("uid", Biz.instance.uid())
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.changePassword(params), object : NetRequestListener<BaseBean<Any?>?> {
            override fun onSuccess(result: BaseBean<Any?>?) {
                getView()?.hideLoading()
                getView()?.handleChangePassword(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleChangePassword(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun boundMobile(mobile: String, captcha: String, password: String) {
        val params = ParamsMap()
        params.putValue("uid", Biz.instance.uid())
        params.putValue("captcha", captcha)
        params.putValue("mobile", mobile)
        params.putValue("password", password)
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.boundMobile(params), object : NetRequestListener<BaseBean<AccountConflict?>?> {
            override fun onSuccess(result: BaseBean<AccountConflict?>?) {
                getView()?.hideLoading()
                getView()?.handleBoundMobile(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleBoundMobile(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun flashBoundMobile(token: String) {
        val params = ParamsMap()
        params.putValue("uid", Biz.instance.uid())
        params.putValue("token", token)
        params.putValue("version", AppTools.getVersionCode())
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.flashBoundMobile(params), object : NetRequestListener<BaseBean<AccountConflict?>?> {
            override fun onSuccess(result: BaseBean<AccountConflict?>?) {
                getView()?.hideLoading()
                getView()?.handleFlashBoundMobile(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleFlashBoundMobile(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

    override fun setMainUser(masterUid: String, slaveUid: String) {
        val params = ParamsMap()
        params.putValue("masterUid", masterUid)
        params.putValue("slaveUid", slaveUid)
        params.putValue("version", AppTools.getVersionCode())
        params.putValue("imei", AppTools.getIMEI())
        params.putValue("type", TypeConstant.PLATFORM)
        ParamsMapUtils.paramsMapMD5(params)
        requestNetwork(ApiServiceUtils.apiService.setMainUser(params), object : NetRequestListener<BaseBean<UserInfor?>?> {
            override fun onSuccess(result: BaseBean<UserInfor?>?) {
                getView()?.hideLoading()
                getView()?.handleSetMainUser(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.handleSetMainUser(null)
                getView()?.hideLoading()
                getView()?.onError(e)
            }
        })
    }

}